from copy import copy

def output_seg_as_py(fn, seg):
    txt='seg=[\n'
    d=0
    for ra,dec in seg:
        txt+='(%.2f,%.2f),' % (ra,dec)
        d+=1
        if d>10:
            txt+='\n'
            d=0
    txt+=']\n'
    f=open(fn,'w')
    f.write(txt)
    print('write to file:%s' % fn)
    f.close()

def output_segs_as_py(fn, segs):
    txt='segs=[\n'
    d=0
    for seg in segs:
        txt+='[\n'
        for ra,dec in seg:
            txt+='(%.2f,%.2f),' % (ra,dec)
            d+=1
            if d>10:
                txt+='\n'
                d=0
        txt+='],\n'
    txt+=']\n'
    f=open(fn,'w')
    f.write(txt)
    print('write to file:%s' % fn)
    f.close()
    
def shift_seg(seg):
    # shift ra = 18 ~ 23 to 42  ~ 47
    ext=[]
    for ra,dec in seg:
        if ra < 23:
            ext.append((ra+24, dec))
    seg += ext
    return ext
    
def merge_raxs_1_0():
    from raxs_1_0 import segs
    seg3t = copy(segs[2])
    seg0 = segs[2]
    seg0.reverse()
    seg5t= copy(segs[0])
    seg1 = segs[0]
    seg1.reverse()
    seg2 = segs[3]
    seg2.reverse()
    seg3t.reverse()
    seg3 = shift_seg(seg3t)
    seg4 = segs[1]
    seg4.reverse()
    seg5t.reverse()
    seg5 = shift_seg(seg5t)    

    seg =seg0
    d=0
    print('seg0 %s .. %s' % (seg0[:3], seg0[-1]))
    print('seg1 %s .. %s' % (seg1[:3], seg1[-1]))
    
    for segx in [seg1,seg2,seg3,seg4,seg5]:
        if abs(segx[0][0] - seg[-1][0]) > 1:
            print('merge fail 1 %d' % d)
            return
        #if abs(segx[0][1] - seg[-1][1]) > 1:
        #    print('merge fail 2 %d' % d)
        #    return
        print('merging %d' % d)
        seg+= segx
        d+=1
            
    #output_as_py('raxs_0.py', seg)
    return seg
    
def merge_raxs_1_1():
    from raxs_1_1 import segs

    seg0 = segs[0]
    seg1 = segs[1]
    seg0.reverse()
    seg1.reverse()
    print('seg0 %s .. %s' % (seg0[:3], seg0[-1]))
    print('seg1 %s .. %s' % (seg1[:3], seg1[-1]))
    
    if abs(seg0[-1][0] - seg1[0][0]) > 1:
        print('merge fail')
        return
        
    if abs(seg0[-1][1] - seg1[0][1]) > 1:
        print('merge fail')
        return
        
    print('merging')
    seg = seg0 + seg1
    
    # extend ra = 18 ~ 23 to 42  ~ 47
    ext=[]
    for ra,dec in seg:
        if ra < 23:
            ext.append((ra+24, dec))
    seg += ext
    #output_as_py('raxs_1.py', seg)
    return seg
        
def get_raxs_1_2():
    from raxs_1_2 import segs
    return segs
def get_raxs_1_3():
    from raxs_1_3 import segs
    return segs
def get_raxs_1_4():
    from raxs_1_4 import segs
    return segs
def get_raxs_1_5():
    from raxs_1_5 import segs
    return segs
def get_raxs_1_6():
    from raxs_1_6 import segs
    return segs
def get_raxs_1_7():
    from raxs_1_7 import segs
    return segs
def get_raxs_1_8():
    from raxs_1_8 import segs
    return segs
def get_raxs_1_9():
    from raxs_1_9 import segs
    return segs

getfs = [get_raxs_1_2,
         get_raxs_1_3,
         get_raxs_1_4,
         get_raxs_1_5,
         get_raxs_1_6,
         get_raxs_1_7,
         get_raxs_1_8,
         get_raxs_1_9,
            ]
            
def merge_raxs_1_n():
    segxs=[]
    d=0
    for getfx in getfs:
        segs = getfx()
        print('%d segs: %d' % (d, len(segs)))
        for seg in segs:
            print('\t seg:%s .. %s (%d)' % (seg[:2], seg[-1], len(seg)))
            segxs.append(seg)
            extx = shift_seg(seg)
            if len(extx) >0:
                segxs.append(extx)
        d+=1
    output_segs_as_py('raxs_1_n.py', segxs)
    
    
def merge_raxs_1():
    seg0 = merge_raxs_1_0()        
    seg1 =merge_raxs_1_1()
    seg1.reverse()
    seg = seg0 + seg1
    output_seg_as_py('raxs_1.py', seg)
    
def get_raxs_2_0():
    from raxs_2_0 import segs
    return segs
def get_raxs_3_0():
    from raxs_3_0 import segs
    return segs
def get_raxs_4_0():
    from raxs_4_0 import segs
    return segs
def get_raxs_5_0():
    from raxs_5_0 import segs
    return segs
    
getnfs = [get_raxs_2_0,
         get_raxs_3_0,
         get_raxs_4_0,
         get_raxs_5_0,
         ]
         
def reportx(fn, seg):
    txt1=''
    txt=''
    ras = list(zip(*seg))[0]
    ra_min = min(ras)
    ra_max = max(ras)
    txt1+='seg :(%.2f,%.2f) ~ (%.2f,%.2f)' %\
                ( seg[0][0],seg[0][1],seg[-1][0],seg[-1][1])
    txt1+= ' ra min:%.2f max:%.2f\n' % (ra_min,ra_max)
    txt+='seg\n'
    for ra,dec in seg:
        txt+='(%.2f,%.2f),' % (ra,dec)
    txt+='\n'
    f=open(fn,'w')
    f.write(txt1)
    f.write(txt)
    f.close()
        
def check_seg_for_gap(seg):
    n=1
    segs=[]
    segx=[seg[0]]
    for i in range(1,len(seg)):
        ra = seg[i][0]
        pra = seg[i-1][0]
        if abs(ra-pra) >2:
            print('check break %.2f %.2f' % (pra,ra))
            segs.append(segx)
            segx=[]
        segx.append(seg[i])
    if len(segx) > 0:
        segs.append(segx)
    #print('len:%d' % len(segs))
    return len(segs), segs
    
def merge_raxs_n_n():
    n=2
    for getnf in getnfs:
        segs = getnf()
        print('%d segs: %d' % (n, len(segs)))
        segxs=[]
        for seg in segs:
            print('\t seg:%s .. %s (%d)' % (seg[:2], seg[-1], len(seg)))
            segxs.append(seg)
            extx = shift_seg(seg)
            if len(extx) >0:
                segxs.append(extx)
        i=0
        seg2xs=[]
        for seg in segxs:
            print(i)
            xn, segxxs = check_seg_for_gap(seg)
            if xn > 1:
                print('i:%d break to %d' % (i, xn))
                for segxx in segxxs:
                    seg2xs.append(segxx)
                continue
            seg2xs.append(seg)
            i+=1
        i=0
        for seg in seg2xs:
            fn = 'raxs_x%d_%dseg.dat' % (n, i)
            reportx(fn, seg)
            i+=1
        output_segs_as_py('raxs_%d_n.py' % n,seg2xs)
        
        n+=1
    #output_segs_as_py('raxs_1_n.py', segxs)
    
merge_raxs_1()
merge_raxs_1_n()
merge_raxs_n_n()




